//
//  BaseScrollViewController.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/14.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "BaseScrollViewController.h"
#import "DefineNavView.h"
#import "BaseTableViewController.h"

@interface BaseScrollViewController ()<UIScrollViewDelegate>


@property (nonatomic, strong) NSMutableArray *myChildViewControllers;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, copy) NSString *notificationName;
@end

@implementation BaseScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];


}

// 加载控件
- (void)loadSubviewsOnHomeWithScrollHeight:(CGFloat)height
                                         y:(CGFloat)y
                                  titleArr:(NSArray *)titleArr{
    UIView *topLineView = [[UIView alloc]init];
    topLineView.backgroundColor = KLColor_B3;
    [self.view addSubview:topLineView];
    [topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(1);
    }];
    self.height = height;
    
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(KLScreenW*titleArr.count, 0);  // 禁止竖直滚动
    [self setUpOneChildViewController:0];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(y);
        make.left.right.equalTo(self.view);
        //KLScreenH-NaviH-TabbarH
        make.height.mas_equalTo(height);
    }];
}

// 配置导航栏的titleView
- (void)configureNavTitleViewWithDataSource:(NSArray *)titleArr
                                      width:(CGFloat)width
                                   distance:(CGFloat)distance
                           notificationName:(NSString *)notificationName{
    // 配置titleView  // 通过宽度和距离和数量计算view的宽度
//    NSArray *titleArr = @[@"推荐",@"最新",@"最火"];
//    CGFloat width = 32;
//    CGFloat distance = 30;
    self.notificationName = notificationName;
    CGFloat navViewW = KNavDefineW(titleArr, width, distance);
    DefineNavView *navView = [[DefineNavView alloc] initWithFrame:CGRectMake(0, 0, navViewW, 44) titleArray:titleArr distance:distance width:width];
    [navView addNotificationWithName:notificationName];
    navView.delegateSignal = [RACSubject subject];
    [navView.delegateSignal subscribeNext:^(NSDictionary *x) {
        [self.scrollView setContentOffset:CGPointMake(KLScreenW * [x[@"index"] integerValue], 0) animated:YES];
        [self setUpOneChildViewController:[x[@"index"] integerValue]];
        
    }];
    navView.backgroundColor = [UIColor whiteColor];
    self.navigationItem.titleView = navView;
    
    
    
}

// 添加自控制器
- (void)addChildControllersWithArray:(NSArray *)controlerNameArr{
    
    for (int i = 0; i < controlerNameArr.count; i++) {
        Class className = NSClassFromString(controlerNameArr[i]);
        KeleBaseViewController *vc = [[className alloc]init];
        [self addChildViewController:vc];
    }

}


// lazyload
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        _scrollView.bounces = YES;
        _scrollView.pagingEnabled = YES;
        _scrollView.contentOffset = CGPointMake(0, 0);
    }
    return _scrollView;
}

// 加载scrollviewView的子控件
- (void)setUpOneChildViewController:(NSInteger)i
{
    
    CGFloat x = i * KLScreenW;
    
    BaseTableViewController *vc;
    
    vc = self.childViewControllers[i];
    
    
    if (vc.view.superview) {
        return;
    }
    vc.view.frame = CGRectMake(x, 0, KLScreenW,self.scrollView.height);
    
    [self.scrollView addSubview:vc.view];
    
    
}

#pragma mark --- scrollview的代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = scrollView.contentOffset.x/KLScreenW;
    // 给scrollView添加内容
    [self setUpOneChildViewController:index];
    [[NSNotificationCenter defaultCenter] postNotificationName:self.notificationName object:@{@"index":@(index)}];
 
}

#pragma mark - getter

- (NSMutableArray *)myChildViewControllers {
    
    if (!_myChildViewControllers) {
        _myChildViewControllers = [NSMutableArray array];
        
    }
    return _myChildViewControllers;
}





- (void)dealloc {
    NSLog(@"父控制器被销毁了");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
